package com.zhang.apipassenger.controller;

import com.zhang.apipassenger.request.OrderRequest;
import com.zhang.apipassenger.service.ServiceOrderService;
import com.zhang.internalcommon.constant.CommonStatusEnum;
import com.zhang.internalcommon.constant.IdentityConstant;
import com.zhang.internalcommon.dto.OrderInfo;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.internalcommon.dto.TokenResult;
import com.zhang.internalcommon.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

/**
 * 类功能描述
 *
 * @author Kye
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/order")
@Validated
public class OrderController {
	@Autowired
	private ServiceOrderService serviceOrderServic;

	@PostMapping("/add")
	public ResponseResult addOrder(@Validated @RequestBody OrderRequest orderRequest) {
		return serviceOrderServic.addOrder(orderRequest);
	}

	@PostMapping("/cancel")
	public ResponseResult cancelOrder(@RequestParam Long orderId) {
		return serviceOrderServic.cancelOrder(orderId);
	}

	@GetMapping("/detail")
	public ResponseResult<OrderInfo> detail(@NotNull(message = "订单id不能为空") @Positive(message = "订单id格式不正确") Long orderId){
		return serviceOrderServic.detail(orderId);
	}

	@GetMapping("/current")
	public ResponseResult<OrderInfo> currentOrder(HttpServletRequest httpServletRequest){
		String authorization = httpServletRequest.getHeader("Authorization");
		TokenResult tokenResult = JwtUtils.parseToken(authorization);
		String identity = tokenResult.getIdentity();
		if (!identity.equals(IdentityConstant.PASSENGER_IDENTITY)){
			return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR.getCode(),CommonStatusEnum.TOKEN_ERROR.getValue());
		}
		String phone = tokenResult.getPhone();

		return serviceOrderServic.currentOrder(phone,IdentityConstant.PASSENGER_IDENTITY);
	}
}
